import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', () => {
 
  const CartList = ref([])
  
  const addCart = (gooods) =>{

    const item  = CartList.value.find(item => item.skuId === gooods.skuId)
    if(item){
      item.count = item.count + gooods.count
    }
    else{
      CartList.value.push(gooods)
    }

  }

  const removeCart = (id) =>{
    CartList.value = CartList.value.filter(item => item.skuId !== id)
  }

  return {
    CartList,
    addCart,
    removeCart
  }
},

{
  persist: true
})
